<?php
// +----------------------------------------------------------------------
// | TYCMS [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: aliang <461321026@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use think\Config;
class Template extends Base
{
    var $index_name = 'home';

    private function getPathArr(){
        $mobandir = Config::get('web_theme');
        $path     = APP_PATH.$this->index_name.'/view/'.$mobandir.'';
        
        $path_arr = [];
        $current_dir = opendir($path);
        while(($file = readdir($current_dir)) !== false) {
            $sub_dir = $path . DIRECTORY_SEPARATOR . $file;
            if($file == '.' || $file == '..') {
                continue;
            }else if(is_dir($sub_dir)) {
                $path_arr[] = str_replace('\\','',str_replace([APP_PATH,'index/view'],'',$sub_dir));
            }
        }   
        return $path_arr;
    }
    
    
    
    
    public function index()
    { 
       $path_arr = $this->getPathArr();
       $mbdir    = input('mbdir');
       $now_path = empty($mbdir) ? $path_arr[1]:base64_decode($mbdir);
       
       $jArr = get_path_file(APP_PATH.$now_path); 
       $jArr = get_path_info($jArr);
       $this->assign([
           'kpath'=>$now_path,
           'jpath'=>$path_arr,
           'list' =>$jArr,
       ]);  
        return $this->fetch();
    }
    
    
    public function add(){
        if(request()->isPost()){
            $param = input('post.');
            if(empty($param['name']))
               return json(['code' => 0, 'data' => '', 'msg' => '模板名称不正确！']);
            if(empty($param['content']))
               return json(['code' => 0, 'data' => '', 'msg' => '模板内容不能为空！']);
            
            $mbdir    = base64_decode($param['mbdir']);
            $path     = APP_PATH.$this->index_name.'/view/'.$mbdir;
            $file_arr = explode('.',$param['name']);
            
            $file_path= $path.'/'.$file_arr[0].'.html';
            if(!file_exists($file_path)){
                al_write($file_path,$param['content']);
                $this->success( '模板添加成功！');
            }else{
                $this->error( '模板添加失败！');
            }
            
        }
        $this->assign([
            'jpath'=>$this->getPathArr()
        ]);
       return $this->fetch();
        
    }
    
    
    
    public function edit(){
        $id    = input('id');
        
        $mbdir = base64_decode($id);
        
        if(request()->isPost()){
            $param = input('post.');
            if(empty($param['name']))
                return json(['code' => 0, 'data' => '', 'msg' => '模板名称不正确！']);
            if(empty($param['content']))
                return json(['code' => 0, 'data' => '', 'msg' => '模板内容不能为空！']);
            
            $mbdir    = base64_decode($param['mbdir']);
            $path     = APP_PATH.$mbdir;
            $file_arr = explode('.',$param['name']);
            
            $file_path= $path.'/'.$file_arr[0].'.html';
            $param['content'] = str_replace('﹍﹍','__',$param['content']);
            al_write($file_path,$param['content']);
            $this->success( '模板修改成功！');
        }
        $fileArr = explode('/',$mbdir);
        $path    = APP_PATH;
        $temp_text = @file_get_contents($path.$mbdir);
        $temp_text = str_replace('__','﹍﹍',$temp_text);
        $this->assign([
            'id'       => $id,
            'mbdir'    => $mbdir,
            'file_name'=> end($fileArr),
            'content'  => $temp_text,
            'kpath'    => str_replace('/'.end($fileArr), '', $mbdir),
            'jpath'    => $this->getPathArr()
        ]);
        return $this->fetch();
    }
    
    
    public function del(){
        $param    = input('param.');
        $id       = isset($param['id']) ? $param['id']:0;
        $path     = APP_PATH.$this->index_name.'/view/';
        if(empty($id)){
            if(request()->isAjax()){
                return json(['code' => 0, 'data' => [], 'msg' => '请选择要删除的模板！']);
            }else{
                $this->error('请选择要删除的模板！');
            }
        }
        
        $isflag = false;
        if(is_array($id)){
            foreach($id as $v){
                $file_path = $path.base64_decode($v);
                if(file_exists($file_path)){
                   @unlink($file_path);
                   $isflag = true;
                }    
            }
        }else{
            $file_path = $path.base64_decode($id);
            if(file_exists($file_path)){
                @unlink($file_path);
                $isflag = true;
            }    
        }
        
        if(request()->isAjax()){
           if($isflag){
               return json(['code' => 1, 'data' => [], 'msg' => '删除模板成功！']);
           }else{
               return json(['code' => 0, 'data' => [], 'msg' => '删除模板失败！']);
           } 
        }else{
           $isflag ? $this->success('删除模板成功！'):$this->error('删除模板失败！');
        }
        
        
    }
}